**********************************
* Replication material for 
* Gessler & Hunger
* "How the refugee crisis and radical right parties shape party competition on immigration"
* PSRM
**********************************


set more off
set scheme s1mono

clear all

* load data
use "stata_data.dta" 

* prepare data
**********************************
* make year-month variable
generate date_m = mofd(date)
format date_m %tm

encode party, gen(partyid)

xtset partyid date_m

gen countryid = 0
replace countryid = 1 if country == "Austria"
replace countryid = 2 if country == "Germany"
replace countryid = 3 if country == "Switzerland"

label define countrylab 1 "Austria" 2 "Germany" 3 "Switzerland"
label values countryid countrylab

gen msp = 0
replace msp = 1 if partfam == "centre right"
replace msp = 1 if partfam == "centre left"

encode crisis, gen(crisisid)

* replace missings with zero
replace sal_rrp = 0 if sal_rrp ==.
replace keyness_predicted_z_rrp = 0 if keyness_predicted_z_rrp == . 
replace keyness_predicted_rrp = 0 if keyness_predicted_rrp == . 

* encode party family variable
encode partfam, gen(partfamid)

* encode crisis2 var
encode crisis2, gen(crisis2id)

* label variables in order to the regression tables look nicer
label var sal "salience of immigration"
label var sal_rrp "salience of RRPs"
label var noref_z "number of asylum applications"
label var keyness_predicted "position on immigration"
label var keyness_predicted_rrp "RRP's position on immigration"


* CR DUMMY
* 2) based on expert knowledge 
* i.e. including liberal parties and BDP
gen CR = 0
replace CR = 1 if partfamid == 2
replace CR = 1 if partfamid == 4

*********************************************************************************
* create interactions by hand - SALIENCE
gen CR_interac = CR*sal_rrp

* create interactions by hand - POSITION
gen CR_interac_pos = CR*keyness_predicted_rrp

********************************************************************

*###################################################################
* MAIN MODELS SALIENCE (table 2, main text)
* ##################################################################

* combined model
xtabond sal sal_rrp L.sal_rrp noref_z polls_ppr refugees_z, vce(robust) lags(2)
outreg2 using model1a, ctitle(all) replace label tex dec(2) pdec(2) 
* test for autocorrelation ************************************************************************
estat abond
* AT
xtabond sal sal_rrp L.sal_rrp noref_z polls_ppr refugees_z if countryid == 1, vce(robust) lags(2)
outreg2 using model1a, ctitle(AT) append  label tex dec(2) pdec(2) 
* DE
xtabond sal sal_rrp L.sal_rrp noref_z polls_ppr refugees_z if countryid == 2, vce(robust) lags(2)
outreg2 using model1a, ctitle(DE) append  label tex dec(2) pdec(2) 
* CH
xtabond sal sal_rrp L.sal_rrp noref_z polls_ppr refugees_z if countryid == 3, vce(robust) lags(2)
outreg2 using model1a, ctitle(CH) append label tex dec(2) pdec(2)  
* before
xtabond sal sal_rrp L.sal_rrp noref_z polls_ppr refugees_z if crisis2id == 2, vce(robust) lags(2)
outreg2 using model1a, ctitle(before) append label tex dec(2) pdec(2) 
* during
xtabond sal sal_rrp L.sal_rrp noref_z polls_ppr refugees_z if crisis2id == 3, vce(robust) lags(2)
outreg2 using model1a, ctitle(during) append label tex dec(2) pdec(2) 
* after
xtabond sal sal_rrp L.sal_rrp noref_z polls_ppr refugees_z if crisis2id == 1, vce(robust) lags(2)
outreg2 using model1a, ctitle(after) append label tex dec(2) pdec(2) 
* center right
xtabond sal sal_rrp L.sal_rrp CR CR_interac L.CR_interac noref_z polls_ppr refugees_z, vce(robust) lags(2)
outreg2 using model1a, ctitle(center-right) append label tex dec(2) pdec(2) 


*###################################################################
* MAIN MODELS POSITION (table 3, main text)
* ##################################################################


* combined model 
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp noref_z polls_ppr refugees_z, vce(robust) lags(2)
outreg2 using model2a, ctitle(all) replace label tex dec(2) pdec(2) 
* test for autocorrelation ************************************************************************
estat abond
* AT
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp noref_z polls_ppr refugees_z if countryid == 1, vce(robust) lags(2)
outreg2 using model2a, ctitle(AT) append  label tex dec(2) pdec(2) 
* DE
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp noref_z polls_ppr refugees_z if countryid == 2, vce(robust) lags(2)
outreg2 using model2a, ctitle(DE) append  label tex dec(2) pdec(2) 
* CH
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp noref_z polls_ppr refugees_z if countryid == 3, vce(robust) lags(2)
outreg2 using model2a, ctitle(CH) append label tex dec(2) pdec(2)
* before
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp noref_z polls_ppr refugees_z if crisis2id == 2, vce(robust) lags(2)
outreg2 using model2a, ctitle(before) append label tex dec(2) pdec(2) 
* during
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp noref_z polls_ppr refugees_z if crisis2id == 3, vce(robust) lags(2)
outreg2 using model2a, ctitle(during) append label tex dec(2) pdec(2) 
* after
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp noref_z polls_ppr refugees_z if crisis2id == 1, vce(robust) lags(2)
outreg2 using model2a, ctitle(after) append label tex dec(2) pdec(2) 
* CR
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp CR CR_interac_pos L.CR_interac_pos noref_z polls_ppr refugees_z, vce(robust) lags(2)
outreg2 using model2a, ctitle(center-right) append label tex dec(2) pdec(2)  


********************************************************************************
* MODELS FOR APPENDIX ##########################################################
********************************************************************************


*************************************************************************************************
* MODELS WITH INTERACTION SEPARATELY FOR COUNTRIES

* SALIENCE
* CR - AT
xtabond sal sal_rrp L.sal_rrp CR CR_interac L.CR_interac noref_z polls_ppr refugees_z if countryid == 1, vce(robust) lags(2)
outreg2 using model1b, ctitle(center-right) replace label tex dec(2) pdec(2) 
* CR - DE
xtabond sal sal_rrp L.sal_rrp CR CR_interac L.CR_interac noref_z polls_ppr refugees_z if countryid == 2, vce(robust) lags(2)
outreg2 using model1b, ctitle(center-right) append label tex dec(2) pdec(2) 
* CR - CH
xtabond sal sal_rrp L.sal_rrp CR CR_interac L.CR_interac noref_z polls_ppr refugees_z if countryid == 3, vce(robust) lags(2)
outreg2 using model1b, ctitle(center-right) append label tex dec(2) pdec(2) 

* POSITION
* CR - AT
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp CR CR_interac_pos L.CR_interac_pos noref_z polls_ppr refugees_z if countryid == 1, vce(robust) lags(2)
outreg2 using model2b, ctitle(center-right) replace label tex dec(2) pdec(2)  
* CR - DE
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp CR CR_interac_pos L.CR_interac_pos noref_z polls_ppr refugees_z if countryid == 2, vce(robust) lags(2)
outreg2 using model2b, ctitle(center-right) append label tex dec(2) pdec(2)
* CR - CH
xtabond keyness_predicted keyness_predicted_rrp L.keyness_predicted_rrp CR CR_interac_pos L.CR_interac_pos noref_z polls_ppr refugees_z if countryid == 3, vce(robust) lags(2)
outreg2 using model2b, ctitle(center-right) append label tex dec(2) pdec(2)

